package com.kylin.common.util;


import com.alibaba.fastjson2.JSONObject;

/**
 * GPS坐标工具类
 * 参考：https://blog.csdn.net/qq_40384985/article/details/84790181
 * <p>
 * 各地图API坐标系统比较与转换;
 * WGS84坐标系：即地球坐标系，国际上通用的坐标系。设备一般包含GPS芯片或者北斗芯片获取的经纬度为WGS84地理坐标系,
 * 谷歌地图采用的是WGS84地理坐标系（中国范围除外）;
 * GCJ02坐标系：即火星坐标系，是由中国国家测绘局制订的地理信息系统的坐标系统。由WGS84坐标系经加密后的坐标系。
 * 谷歌中国地图和搜搜中国地图采用的是GCJ02地理坐标系; BD09坐标系：即百度坐标系，GCJ02坐标系经加密后的坐标系;
 * 搜狗坐标系、图吧坐标系等，估计也是在GCJ02基础上加密而成的。
 * <p>
 * <p>
 * WGS-84 地心坐标系，即GPS原始坐标体系
 * GCJ-02火星坐标系，国测局02年发布的坐标体系，它是一种对经纬度数据的加密算法，即加入随机的偏差。高德、腾讯、Google中国地图使用。国内最广泛使用的坐标体系；
 * 一般都是由GCJ-02进过偏移算法得到的。这种体系就根据每个公司的不同，坐标体系都不一样了。
 * 比如，百度的BD-09坐标、搜狗坐标等
 */
public class GPSUtils {

    public static double pi = 3.1415926535897932384626;
    public static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
    public static double a = 6378245.0;
    public static double ee = 0.00669342162296594323;

    public static double transformLat(double x, double y) {
        double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y
                + 0.2 * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0;
        return ret;
    }

    public static double transformLon(double x, double y) {
        double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1
                * Math.sqrt(Math.abs(x));
        ret += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0
                * pi)) * 2.0 / 3.0;
        return ret;
    }

    public static boolean outOfChina(double lat, double lon) {
        if (lon < 72.004 || lon > 137.8347)
            return true;
        if (lat < 0.8293 || lat > 55.8271)
            return true;
        return false;
    }

    /**
     * 84 to 火星坐标系 (GCJ-02) World Geodetic System ==> Mars Geodetic System
     *
     * @param lat
     * @param lon
     * @return
     */
    public static double[] gps84_To_Gcj02(double lat, double lon) {
        if (outOfChina(lat, lon)) {
            return new double[]{lat, lon};
        }
        double dLat = transformLat(lon - 105.0, lat - 35.0);
        double dLon = transformLon(lon - 105.0, lat - 35.0);
        double radLat = lat / 180.0 * pi;
        double magic = Math.sin(radLat);
        magic = 1 - ee * magic * magic;
        double sqrtMagic = Math.sqrt(magic);
        dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
        dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi);
        double mgLat = lat + dLat;
        double mgLon = lon + dLon;
        return new double[]{mgLat, mgLon};
    }

    /**
     * * 火星坐标系 (GCJ-02) to 84 * * @param lon * @param lat * @return
     */
    public static double[] gcj02_To_Gps84(double lat, double lon) {
        double[] gps = gps84_To_Gcj02(lat, lon);
        double lontitude = lon * 2 - gps[1];
        double latitude = lat * 2 - gps[0];
        return new double[]{latitude, lontitude};
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 将 GCJ-02 坐标转换成 BD-09 坐标
     *
     * @param lat
     * @param lon
     */
    public static double[] gcj02_To_Bd09(double lat, double lon) {
        double x = lon, y = lat;
        double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta) + 0.0065;
        double tempLat = z * Math.sin(theta) + 0.006;
        double[] gps = {tempLat, tempLon};
        return gps;
    }

    /**
     * * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换算法 * * 将 BD-09 坐标转换成GCJ-02 坐标 * * @param
     * bd_lat * @param bd_lon * @return
     */
    public static double[] bd09_To_Gcj02(double lat, double lon) {
        double x = lon - 0.0065, y = lat - 0.006;
        double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        double tempLon = z * Math.cos(theta);
        double tempLat = z * Math.sin(theta);
        double[] gps = {tempLat, tempLon};
        return gps;
    }

    /**
     * 将gps84转为bd09
     *
     * @param lat
     * @param lon
     * @return
     */
    public static double[] gps84_To_bd09(double lat, double lon) {
        double[] gcj02 = gps84_To_Gcj02(lat, lon);
        double[] bd09 = gcj02_To_Bd09(gcj02[0], gcj02[1]);
        return bd09;
    }

    public static JSONObject bd09_To_gps84(double lat, double lon) {
        double[] gcj02 = bd09_To_Gcj02(lat, lon);
        double[] gps84 = gcj02_To_Gps84(gcj02[0], gcj02[1]);
        //保留小数点后六位
        gps84[0] = retain6(gps84[0]);
        gps84[1] = retain6(gps84[1]);

        JSONObject point = new JSONObject();
        point.put("lat", gps84[0]);
        point.put("lng", gps84[1]);
        return point;
    }

    /**
     * 保留小数点后六位
     *
     * @param num
     * @return
     */
    private static double retain6(double num) {
        String result = String.format("%.6f", num);
        return Double.valueOf(result);
    }

    public static void main(String[] args) throws Exception {
//        String police001 = FileUtils.readFileToString(new File("D://gis/010.json"),"UTF-8");
//        JSONArray points = JSONArray.parseArray(police001);
//        JSONArray newPoints = new JSONArray();
//        for(int i = 0;i < points.size();i ++) {
//            JSONObject baiduPoint = points.getJSONObject(i);
//            newPoints.add(GPSUtils.bd09_To_gps84(baiduPoint.getDouble("lat"), baiduPoint.getDouble("lng")));
//        }
//        System.out.println(newPoints.toJSONString());

//        String sss = "118.815126,32.06397";
//        String[] array = sss.split(",");
//        JSONObject result = GPSUtils.bd09_To_gps84(Double.parseDouble(array[1]), Double.parseDouble(array[0]));
//        System.out.println(String.format("[%s,%s]",result.get("lng"),result.get("lat")));

        double[] jso = gps84_To_bd09(31.9813,118.800119);
        System.out.println(JSONObject.toJSONString(jso));
//            String ss = "[118.75119324445,32.019688747628],[118.75116539698,32.019704055106],[118.75123815973,32.019797430664],[118.75125253262,32.019820391853],[118.75124714278,32.019841056917],[118.75118336309,32.019870141074],[118.75118336309,32.019870141074],[118.75118336309,32.019870141074],[118.75114383765,32.01988851001],[118.75114383765,32.01988851001],[118.75114383765,32.01988851001],[118.75096776977,32.019626752316],[118.75043058308,32.018877447608],[118.75027876944,32.018660844583],[118.75027876944,32.018660844583],[118.75027876944,32.018660844583],[118.75065875267,32.018504706393],[118.75256046544,32.017726307585],[118.75256046544,32.017726307585],[118.75256046544,32.017726307585],[118.75199453297,32.016908101443],[118.75123187159,32.015793675972],[118.75119144784,32.015743924516],[118.75119144784,32.015743924516],[118.75119144784,32.015743924516],[118.75079978664,32.015203545404],[118.74980715905,32.013834213388],[118.74977392175,32.013787522553],[118.74904629428,32.012785577362],[118.74894298915,32.012643206865],[118.74863397206,32.012223748404],[118.74853605675,32.012093624035],[118.7477931581,32.011108499965],[118.74761978513,32.010878865778],[118.74773656485,32.010819926243],[118.74773656485,32.010819926243],[118.74773656485,32.010819926243],[118.74831956513,32.010519104384],[118.74855851439,32.010401224808],[118.74863127714,32.010364483091],[118.74870942972,32.010323914094],[118.74878488738,32.010284875986],[118.74894388746,32.010206034265],[118.74910109092,32.01012948884],[118.74925020964,32.010052177895],[118.74932746391,32.010013905125],[118.7511519224,32.00910836669],[118.75170707521,32.008832034059],[118.75186697359,32.008753191076],[118.75220832968,32.00858785023],[118.75219934662,32.00857483728]";
//          String[] options = ss.split("],");
//          for(String item: options){
//              String[] array = item.replace("[","").replace("]","").split(",");
//              JSONObject result = GPSUtils.bd09_To_gps84(Double.parseDouble(array[1]), Double.parseDouble(array[0]));
//               System.out.print(String.format("[%s,%s],",result.get("lng"),result.get("lat")));
//          }
    }
}

